<?
class CheckoutController extends BaseController {
    var $step;
    var $total_step = 3;
    var $cart;
    public function __construct(){
        parent::__construct();
        $this->checkUserLogin();
        $this->step = getValue('step','int','POST',1,3);
        $this->cart = json_decode(base64_decode(getValue('cart','str','COOKIE','',3)),1);
    }

    public function getBreadCrumb(){
        $array = array('url'=>generate_checkout_url());
        return CheckoutLayout::generate_breadcrumb($array);
    }

    /*
     * Kiểm tra đăng nhập - nếu chưa đăng nhập thì cho chuyển đến trang đăng nhập
     */
    public function checkUserLogin(){
        $myuser = new user();
        $url_return = $_SERVER['REQUEST_URI'];
        $link_redirect = link_redirect(generate_login_url(), $url_return);
        if(!$myuser->logged()){
            redirect($link_redirect);
        }else{
            return true;
        }

    }

    /*
     * Hàm lấy ra phần header bước thanh toán
     */
    public function getStepCheckout(){
        if($this->step) {
            switch($this->step){
                case 1:
                    $array = array('step'=>1,'title'=>'Kiểm tra thông tin đơn hàng','total'=>$this->total_step);
                    break;
                case 2:
                    $array = array('step'=>2,'title'=>'Thanh toán đơn hàng','total'=>$this->total_step);
                    break;
            }

            $html = CheckoutLayout::step_checkout($array);
            return $html;
        }
    }
    public function getReviewInfoUser(){
        $myuser = new user();
        if($myuser->logged()){
            $array = array(
                'name'=>$myuser->fullname,
                'email'=>$myuser->email,
                'phone'=>$myuser->phone,
                'address'=>$myuser->contact
            );
            return CheckoutLayout::review_info_user($array);
        }
    }
    public function getReviewInfoCart(){
        if(!$this->cart){
            return ;
        }
        $total = 0;
        $list = array();
        foreach($this->cart as $k=>$v){
            $db_query = new db_query('SELECT pro_id,
                                             pro_thumb,
                                             pro_name,
                                             pro_price
                                      FROM   products
                                      WHERE  pro_id = '.$k);
            $row = mysql_fetch_assoc($db_query->result);unset($db_query);
            $subtotal = (int) ($row['pro_price'] * $v);
            $total += $subtotal;
            $tmp = array(
                'thumb'=>PATH_PICTURE_PRODUCT_SMALL . $row['pro_thumb'],
                'name'=>$row['pro_name'],
                'link'=>generate_product_detail_url($row),
                'price'=>format_price($row['pro_price']),
                'sub_total'=>format_price($subtotal),
                'num'=> $v
            );
            $list[] = $tmp;
        }
        return CheckoutLayout::review_info_cart(array('total'=>format_price($total), 'list'=>$list));
    }

    public function getPaymentOption(){

    }

    public function getUrlBaokimPayment($params){
        $email = 'dangtrungkien89@gmail.com';
        $product_name = $params['name'];
        $product_price = $params['price'];
        $total_amount = $params['total'];
        $success_url = $params['success_url'];
        $cancel_url = $params['cancel_url'];
        $detail_url = $params['detail_url'];
        $url = 'https://www.baokim.vn/payment/product/version11?business='.$email
                            .'&product_name='.$product_name.'&product_price='.$product_price
                            .'&product_quantity=1&total_amount='.$total_amount
                            .'&url_detail='.$detail_url.'&url_success='.$success_url.'&url_cancel='.$cancel_url
                            .'&product_description=Thanh toán đơn hàng cho Fonecare.vn';
        return $url;
    }

    public function getNextStepButton(){
        switch($this->step){
            case 1:
                $title = 'Thanh toán đơn hàng';
                break;
            case 2:
                $title = 'Xác nhận thanh toán';
                break;

        }
        if($this->step == 3){
            return '';
        }
        $html = '<form method="post" action="">
                    <input type="hidden" name="step" value="'.($this->step + 1).'">
                    <div class="checkout_bottom">
                        <button class="button btn_thanhtoan" type="submit"><span>Bước '.($this->step + 1).' : '.$title.'</span></button>
                    </div>
                </form>';
        return $html;
    }
}
?>